package com.tongzhan.util;

import cn.minsin.core.tools.StringUtil;
import com.tongzhan.base.BaseCriteria;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {

    public static BaseCriteria getDateSearchCriteria(String start_date, String end_date, String addTimeKey) {
        if (StringUtil.isAllBlank(start_date, end_date)) {
            return new BaseCriteria();
        }
        Date startDate = null;
        Date endDate = null;
        if (StringUtil.isNotBlank(start_date)) {
            startDate = cn.minsin.core.tools.DateUtil.string2Date(start_date + " 00:00:00", cn.minsin.core.tools.DateUtil.DateFormatEnum.yyyy_MM_dd_HH_mm_ss);
        }
        if (StringUtil.isNotBlank(end_date)) {
            endDate = cn.minsin.core.tools.DateUtil.string2Date(end_date + " 23:59:59", cn.minsin.core.tools.DateUtil.DateFormatEnum.yyyy_MM_dd_HH_mm_ss);
        }
        return BaseCriteria.where(addTimeKey).between(startDate, endDate, true);
    }

    public static String getStartDate(Integer type, String date) {
        String startDate = "";
        if (type == 1) {
            startDate = date + "-01-01";
        } else {
            startDate = date + "-01";
        }
        return startDate;
    }

    public static String getEndDate(Integer type, String date) {
        String endDate = "";
        if (type == 1) {
            endDate = date + "-12-31";
        } else if (type == 2) {
            if ("01".equals(date.substring(5))) {
                if (date.length() > 4) {
                    endDate = date.substring(0, 4) + "-06-30";
                }
            } else {
                if (date.length() > 4) {
                    endDate = date.substring(0, 4) + "-12-31";
                }
            }
        } else if (type == 3) {
            endDate = getFourSeasonsEndDate(date);
        } else if (type == 4) {
            endDate = getMonthEndDate(date);
        }
        return endDate;
    }

    public static String getFourSeasonsEndDate(String date) {
        String endDate = "";
        String year = date.substring(0, 4);
        String month = date.substring(5);
        switch (month) {
            case "01":
                endDate = year + "-03-31";
                break; //可选
            case "04":
                endDate = year + "-06-31";
                break; //可选
            case "07":
                endDate = year + "-09-31";
                break; //可选
            case "10":
                endDate = year + "-12-31";
                break;
        }
        return endDate;
    }

    public static String getMonthEndDate(String date) {
        String endDate = "";
        String year = date.substring(0, 4);
        String month = date.substring(5);
        switch (month) {
            case "01":
                endDate = year + "-01-31";
                break;
            case "02":
                endDate = year + "-02-29";
                break;
            case "03":
                endDate = year + "-03-31";
                break;
            case "04":
                endDate = year + "-04-30";
                break;
            case "05":
                endDate = year + "-05-31";
                break;
            case "06":
                endDate = year + "-06-30";
                break;
            case "07":
                endDate = year + "-07-31";
                break;
            case "08":
                endDate = year + "-08-31";
                break;
            case "09":
                endDate = year + "-09-30";
                break;
            case "10":
                endDate = year + "-10-31";
                break;
            case "11":
                endDate = year + "-11-30";
                break;
            case "12":
                endDate = year + "-12-31";
                break;
        }
        return endDate;
    }

    public static String getSysYear() {
        Calendar date = Calendar.getInstance();
        String year = String.valueOf(date.get(Calendar.YEAR));
        return year;

    }
}
